package com.firebird.app.fekpsl.ui.fragment.me

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.firebird.app.fekpsl.R
import im.dacer.androidcharts.LineView
import java.util.*

@SuppressLint("ValidFragment")
class SimpleCardFragment : Fragment() {
    private var mTitle: String? = null
    private var randomint = 30
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val v: View = inflater.inflate(R.layout.me_sta_fr_simple_card, null)
        val lineView = v.findViewById<View>(R.id.line_view) as LineView
        initLineView(lineView)
        randomSet(lineView)
        return v
    }

    private fun initLineView(lineView: LineView) {
        val test = ArrayList<String>()
        for (i in 0 until randomint) {
            test.add((i + 1).toString())
        }
        lineView.setBottomTextList(test)
        lineView.setColorArray(intArrayOf(
                Color.parseColor("#F44336"), Color.parseColor("#9C27B0"),
                Color.parseColor("#2196F3"), Color.parseColor("#009688")
        ))
        lineView.setDrawDotLine(true)
        lineView.setShowPopup(LineView.SHOW_POPUPS_NONE)
    }

    private fun randomSet(lineView: LineView) {
        val dataList = ArrayList<Int>()
        var random = (Math.random() * 9 + 1).toFloat()
        for (i in 0 until randomint) {
            dataList.add((Math.random() * random).toInt())
        }
        val dataList2 = ArrayList<Int>()
        random = (Math.random() * 9 + 1) as Float
        for (i in 0 until randomint) {
            dataList2.add((Math.random() * random).toInt())
        }
        val dataLists = ArrayList<ArrayList<Int>>()
        dataLists.add(dataList)
        dataLists.add(dataList2)
        lineView.setDataList(dataLists)
    }

    companion object {
        fun getInstance(title: String?): SimpleCardFragment {
            val sf = SimpleCardFragment()
            var days = 10
            when (title) {
                "30天" -> days = 3
                "90天" -> days = 9
                "1年" -> days = 365
                "所有时间" -> days = 500
            }
            sf.mTitle = title
            sf.randomint = days
            return sf
        }
    }
}
